﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XYZShell.CSHelper
{
    public static class CollectionsHelper
    {
        /// <summary>
        /// 集合传字典
        /// </summary>
        /// <typeparam name="T">集合元素类型</typeparam>
        /// <typeparam name="Key">key类型</typeparam>
        /// <typeparam name="Value">value类型</typeparam>
        /// <param name="datas">数据</param>
        /// <param name="getKey">获取key方法</param>
        /// <param name="getValue">获取value方法</param>
        /// <returns></returns>
        public static Dictionary<Key, Value> ToDictionary<T, Key, Value>(this IEnumerable<T> datas, Func<T,Key> getKey, Func<T,Value> getValue)
        {
            var rs = new Dictionary<Key, Value>();
            if (datas?.Any() != true)
            {
                return rs;
            }
            foreach (var item in datas)
            {
                var key = getKey(item);
                var value = getValue(item);
                rs[key] = value;
            }
            return rs;
        }
    }
}
